package com.wsc.crawler.grabber;

import java.io.IOException;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;

import com.wsc.crawler.URLutils.URLBean;
import com.wsc.crawler.postcrawling.ConstructXML;
import com.wsc.crawler.postcrawling.PostURLs;

public class HandleHTTPResponse {
    HttpResponse response=null;
    private int STATUS_CODE = 0;
    private String RESPONSE_MESSAGE=null;
	private URLBean baseUrl=null; 
    
	public HandleHTTPResponse(HttpResponse response, URLBean baseUrlBean) {
		this.response = response;
		this.STATUS_CODE=response.getStatusLine().getStatusCode();
		this.RESPONSE_MESSAGE=response.getStatusLine().getReasonPhrase();
		this.baseUrl=baseUrlBean;
		printToConsole();
	}
    
	public boolean checkStatus(){
		return (this.STATUS_CODE==200?true:false);
	}
	
	public void handleErrorCode(){

		switch(this.getStatusCode())
		{
		case 201:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 202:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 204:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
			
		/*
		 * Client Error codes 4xx
		 * 
		 * */
			
		case 400: {
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		}
		case 401:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 402:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 403:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 404:
			//code to index this url
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 405:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 406:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
			/*
			 * Server Error Codes 5xx 
			 */
		case 500:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 501:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 502:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 503:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		case 504:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		
		case 505:
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
			break;
		
		default: 			
			System.out.println(this.getStatusCode()+" "+this.RESPONSE_MESSAGE);
		}

		
	}
	
	public void printToConsole(){
		if(checkStatus())
		{		
			/*
			 *Write parsing HTMl code here. 
			 */
			Header contenttype=this.response.getFirstHeader("Content-Type");
		
		// if content type is text/*, then parse html
		if(contenttype.getValue().indexOf("text") >= 0){
		try {
			System.out.println("Content type from "+baseUrl.toString()+" is "+contenttype);
			
			HyperLinkParser linkparser=new HyperLinkParser(EntityUtils.toString(this.response.getEntity()),baseUrl.getOriginalURL().toString());
			
			List<String> urls=linkparser.getAllLinksInPage();
			ConstructXML xml=new ConstructXML();
			String xmlfile=xml.constructXML(urls);
			// print to console
			System.out.println(xmlfile);
			
			PostURLs post=new PostURLs();
			//post constructed xml file to frontier, change httphost to get from config.
			post.postXML(xmlfile, new HttpHost("127.0.0.1", 8080));
			/*
			 * get list of links from parser class 
			 * and 
			 * Filter for WSDL URLS, if found export to xml file and post to Frontier server's WSDL table.
			 * 
			 * for general urls,
			 * export to XML file
			 * 
			 * then Post to Frontier server
			 * 
			 */
		} catch (ParseException e) {
	  
			e.printStackTrace();
		} catch (IOException e) {
	 
			e.printStackTrace();
		}
		
		}else{
			System.out.println("Skipping response from "+baseUrl.toString()+" because content type is "+contenttype);
		}
	}else{
		// status code is not 200
		handleErrorCode();
	}
	}
		
	public int getStatusCode() {
		return STATUS_CODE;
	}

	 
}
